package com.lejin.interceptor;


import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class UserInfoInterceptor implements HandlerInterceptor {



    private ThreadLocal<String> threadLocal=new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String userId = request.getHeader("userId");
        if (!StringUtils.isEmpty(userId)){
            //将用户Id传入线程中
            threadLocal.set("3");
        }
        threadLocal.set("3");
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        threadLocal.remove();
    }



    public ThreadLocal<String> getThreadLocal(){

        return threadLocal;
    }


    public void setThreadLocal(ThreadLocal<String> threadLocal){

        this.threadLocal=threadLocal;
    }
}
